package com.zgjkhis.modules.system.service.mapper;

import com.zgjkhis.common.mapper.CoreMapper;
import com.zgjkhis.modules.charges.service.dto.InfoDto;
import com.zgjkhis.modules.system.domain.Info;
import com.zgjkhis.modules.system.domain.vo.InfoMarketTopSaleVo;
import com.zgjkhis.modules.system.domain.vo.InfoSaleVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author yzq
 * @Description 处方
 * @Date 2021/6/23 9:48
 * @Param
 * @return
 **/
@Repository
public interface InfoMapper extends CoreMapper<Info> {


    @Select("<script>select t.pst_type,t.adjust_time,t.prescription_type,t.total_pay,t.consult_no,t.pay_status,t.id,t.order_condition,t.dosage_form,t.source from his_prescription_info t where t.order_no=#{orderNo} and delete_flag=0 </script>")
    List<InfoDto> queryListByOrderNo(@Param("orderNo") String orderNo);


    @Select("<script>select\n" +
            " a.doc_name, \n" +
            " a.doc_id, \n" +
            " cast(a.create_time as date) as date, \n" +
            " count(*) as allInfoNum, \n" +
            " sum(case  when a.pay_status  in('1','2') then 1  else 0 end) as payInfoNum, \n" +
            " sum(case  when a.pay_status  in('2','3') then 1  else 0 end) as refundInfoNum, \n" +
            " sum(case  when prescription_type=2 then 1  else 0 end) as xyInfoNum, \n" +
            " sum(case  when prescription_type=1 then 1  else 0 end) as ypInfoNum, \n" +
            " sum(case  when prescription_type=3 then 1  else 0 end) as kljInfoNum, \n" +
            " sum(case  when a.pay_status  in('1','2') and prescription_type=2 then 1  else 0 end) as xyPayInfoNum, \n" +
            " sum(case  when a.pay_status  in('1','2') and prescription_type=1 then 1  else 0 end) as ypPayInfoNum, \n" +
            " sum(case  when a.pay_status  in('1','2') and prescription_type=3 then 1  else 0 end) as kljPayInfoNum, \n" +
            " sum(case  when a.pay_status  in('1','2','3') and prescription_type=2 then (a.drug_cost-a.course_cost-a.cirulation_cost)*b.drug_discount*b.all_discount  else 0 end) as xyAllInfoFee, \n" +
            " sum(case  when a.pay_status  in('1','2','3') and prescription_type=1 then (a.drug_cost-a.course_cost-a.cirulation_cost)*b.drug_discount*b.all_discount  else 0 end) as ypAllInfoFee, \n" +
            " sum(case  when a.pay_status  in('1','2','3') and prescription_type=3 then (a.drug_cost-a.course_cost-a.cirulation_cost)*b.drug_discount*b.all_discount  else 0 end) as kljAllInfoFee, \n" +
            " sum(case  when a.pay_status  in('1','2','3')  then (a.drug_cost-a.course_cost-a.cirulation_cost)*b.drug_discount*b.all_discount  else 0 end) as allFee " +
            "  from his_prescription_info a LEFT JOIN his_prescription_order b on a.order_no=b.order_no   where   a.create_time BETWEEN #{startDate} and #{endDate} and a.pharmacy_id=#{pharmacyId} " +
            "<if test=\"docName !=null and docName!=''\">\n" +
            "and a.doc_name=#{docName}\n" +
            "</if>\n" +
            "<if test=\"type =='doc'\">\n" +
            "GROUP BY a.doc_id" +
            "</if>" +
            "<if test=\"type == 'date'\">\n" +
            "GROUP BY cast(a.create_time as date)" +
            "</if>"+
            "</script>")
    List<InfoSaleVo> infoStatistics(@Param("pharmacyId") String pharmacyId, @Param("startDate") String startDate, @Param("endDate") String endDate, @Param("type") String type, @Param("docName") String docName);

    @Select("<script>SELECT\n" +
            "b.doc_name," +
            "b.doc_id," +
            "cast(b.create_time as date) as date," +
            "sum(case when a.refund_type=1 then a.refund_price  else 0 end) as refundFee," +
            "sum(case when b.prescription_type=2 and a.refund_type=1 then a.refund_price  else 0 end) as xyRefundFee," +
            "sum(case when b.prescription_type=1 and a.refund_type=1 then a.refund_price  else 0 end) as ypRefundFee," +
            "sum(case when b.prescription_type=3 and a.refund_type=1 then a.refund_price  else 0 end) as kljRefundFee\n" +
            "FROM\n" +
            "his_prescription_refund_detail a," +
            "his_prescription_info b " +
            "WHERE" +
            " b.pharmacy_id=#{pharmacyId} and b.create_time BETWEEN #{startDate} and #{endDate} and a.info_no = b.info_no" +
            "<if test=\"type =='doc'\">\n" +
            "GROUP BY b.doc_id" +
            "</if>" +
            "<if test=\"type =='date'\">\n" +
            "GROUP BY cast(b.create_time as date)" +
            "</if>"+
            "</script>")
    List<InfoSaleVo> infoRefundStatistics(@Param("pharmacyId") String pharmacyId, @Param("startDate") String startDate, @Param("endDate") String endDate, @Param("type") String type);

    @Select("<script>SELECT\n" +
            "cast(b.create_time as date) as date,"+
            "sum(case  when b.prescription_type=2  then (c.plus_num)*(d.cost_price)  end) as xyCost," +
            "sum(case  when b.prescription_type=2  then (c.plus_num)*(a.discount_price*e.drug_discount*e.all_discount-d.cost_price)  end) as xyProfit," +
            "sum(case  when b.prescription_type=1  then (c.plus_num)*(d.self_price)  end) as ypCost," +
            "sum(case  when b.prescription_type=1  then (c.plus_num)*(a.discount_price*e.drug_discount*e.all_discount-d.self_price)  end) as ypProfit," +
            "sum(case  when b.prescription_type=3  then (c.plus_num)*(d.self_price)  end) as kljCost," +
            "sum(case  when b.prescription_type=3  then (c.plus_num)*(a.discount_price*e.drug_discount*e.all_discount-d.self_price)  end) as kljProfit" +
            " FROM\n" +
            "his_prescription_info_drug a," +
            "his_prescription_info b ," +
            "his_prescription_drug_comfire c," +
            "his_drug_warehousing_detail d, " +
            "his_prescription_order e" +
            " WHERE" +
            " a.info_no = b.info_no AND" +
            " c.info_drug_id=a.id AND" +
            " c.warehousing_id=d.id AND" +
            " b.order_no=e.order_no AND " +
            " a.is_cirulation='0' AND " +
            " b.pay_status  in('1','2','3')" +
            " and b.pharmacy_id=#{pharmacyId} and b.create_time BETWEEN #{startDate} and #{endDate}" +
            " GROUP BY cast(b.create_time as date)"+
            "</script>"
    )
    List<InfoSaleVo> infoDrugCostStatistics(@Param("pharmacyId") String pharmacyId, @Param("startDate") String startDate, @Param("endDate") String endDate, @Param("type") String type);




    @Select("<script>select \n" +
            "count(case when info.pay_status  in('1','2') then 1 end)  as payInfoNum,\n" +
            "count(*) as allInfoNum,\n" +
            "count(case when info.pay_status  in('3','2') then 1 end)  as cancelInfoNum,\n" +
            "(select sum(refund_price) from his_prescription_refund_detail where pharmacy_id=#{pharmacyId} and refund_type=1) as cancelFee,\n" +
            "sum(case when info.pay_status  in('1','2','3') then (info.drug_cost-info.course_cost-info.cirulation_cost)*f.drug_discount * f.all_discount end)  as allFee,\n" +
//            "(select sum(o.total_fee+o.other_fee-o.actual_charge) from his_prescription_order o where  pharmacy_id=#{pharmacyId} and o.pay_status  in('1','2','3','5','6')) as discountFee,\n" +
            "(select sum((a.price-a.price*o.all_discount*o.drug_discount)*(a.quantity-a.refund_count)) from his_prescription_info_drug a,his_prescription_order o where a.order_no=o.order_no and a.status in ('1','2') and a.is_cirulation='0' and a.pharmacy_id=#{pharmacyId}) + sum(f.diff_fee) as discountFee, "+
            "e.*\n" +
            "from his_prescription_info info,\n" +
            " his_prescription_order f,\n" +
            " (SELECT sum(c.plus_num*d.self_price) as costFee  \n" +
            "FROM\n" +
            "his_prescription_info_drug a,\n" +
            "\this_prescription_drug_comfire c,\n" +
            "\this_drug_warehousing_detail d\n" +
            "WHERE\n" +
            "\tc.warehousing_id=d.id AND\tc.info_drug_id=a.id  and a.pharmacy_id=#{pharmacyId} and a.is_cirulation='0' ) e\n" +
            "where info.pharmacy_id=#{pharmacyId} and info.order_no=f.order_no"+
            "</script>")
    InfoMarketTopSaleVo infoMarketTop(@Param("pharmacyId") String pharmacyId);



}
